import os
import re

def scan_handler_methods(directory, output_file):
    method_pattern = re.compile(r"pub\s+async\s+fn\s+(\w+)\s*\(")
    handler_files = [f for f in os.listdir(directory) if f.endswith("_handler.rs")]

    with open(output_file, "w", encoding="utf-8") as output:
        for file in handler_files:
            file_name = file.replace(".rs", "")  # 去掉 .rs 后缀
            file_path = os.path.join(directory, file)
            with open(file_path, "r", encoding="utf-8") as f:
                content = f.read()
                methods = method_pattern.findall(content)
                if methods:
                    output.write(f"// Methods in {file_name}\n")
                    for method in methods:
                        output.write(f"{file_name}::{method},\n")
                    output.write("\n")
        print(f"Methods have been written to {output_file}")

if __name__ == "__main__":
    handlers_dir = "./src/handlers"  
    output_file = "./tools/handler_methods.txt"
    if os.path.exists(handlers_dir):
        scan_handler_methods(handlers_dir, output_file)
    else:
        print(f"Directory {handlers_dir} does not exist.")